package com.agriculture.mapper;

import com.agriculture.domain.Product;
import com.agriculture.domain.OrderItem;
import com.agriculture.dto.InventoryProductDTO;
import org.apache.ibatis.annotations.*;
import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ProductMapper {
    int insert(Product product);

    @Select("SELECT * FROM Products WHERE product_id = #{productId}")
    Product findById(Long productId);

    @Select("SELECT * FROM Products WHERE owner_id = #{ownerId} AND owner_type = #{ownerType}")
    List<Product> findByOwner(@Param("ownerId") Long ownerId, @Param("ownerType") String ownerType);

    int adjustStock(@Param("productId") Long productId, @Param("amount") Integer amount);

    int updateStock(@Param("productId") Long productId, @Param("stock") Integer stock);

    @Update("UPDATE Products SET status = #{status} WHERE product_id = #{productId}")
    int updateStatus(@Param("productId") Long productId, @Param("status") String status);

    @Select("SELECT * FROM Products WHERE category = #{category} AND status = 'on_sale'")
    List<Product> findByCategory(String category);

    List<Product> searchProducts(@Param("keyword") String keyword, @Param("category") String category);

    /**
     * 分页查询商品列表
     */
    List<Product> getProductsByPage(@Param("ownerId") Long ownerId,
                                  @Param("ownerType") String ownerType,
                                  @Param("keyword") String keyword,
                                  @Param("category") String category,
                                  @Param("offset") Integer offset,
                                  @Param("pageSize") Integer pageSize);


    List<Product> findProducts(@Param("ownerId") Long ownerId,
                             @Param("ownerType") String ownerType,
                             @Param("keyword") String keyword,
                             @Param("category") String category,
                             @Param("offset") Integer offset,
                             @Param("pageSize") Integer pageSize);
                             
    /**
     * 获取商品总数
     */
    int getProductsCount(@Param("ownerId") Long ownerId,
                        @Param("ownerType") String ownerType,
                        @Param("keyword") String keyword,
                        @Param("category") String category);

    @Update("UPDATE Products SET " +
            "product_name = #{productName}, " +
            "type = #{type}, " +
            "category = #{category}, " +
            "price = #{price}, " +
            "selling_price = #{sellingPrice}, " +
            "unit = #{unit}, " +
            "description = #{description}, " +
            "image_url = #{imageUrl} " +
            "WHERE product_id = #{productId}")
    int update(Product product);

    @Delete("DELETE FROM Products WHERE product_id = #{productId}")
    int delete(Long productId);

    /**
     * 查询可购买的农产品列表（使用XML配置）
     */
    List<Product> selectPurchaseProducts(
        @Param("search") String search,
        @Param("category") String category,
        @Param("ownerName") String ownerName,
        @Param("minPrice") BigDecimal minPrice,
        @Param("maxPrice") BigDecimal maxPrice,
        @Param("sortBy") String sortBy,
        @Param("sortOrder") String sortOrder
    );

    /**
     * 批量更新商品库存
     */
    int batchUpdateStock(@Param("items") List<OrderItem> items);

    /**
     * 根据主键查询商品（包含供应商信息）
     */
    Product selectByPrimaryKey(Long id);

    /**
     * 查询可用于添加商品的库存列表
     */
    List<InventoryProductDTO> selectInventoryProducts(
        @Param("search") String search,
        @Param("category") String category,
        @Param("userId") Long userId,
        @Param("offset") Integer offset,
        @Param("pageSize") Integer pageSize
    );

    /**
     * 统计符合条件的库存商品总数
     */
    int countInventoryProducts(
        @Param("search") String search,
        @Param("category") String category,
        @Param("userId") Long userId
    );

    List<Product> getMarketProducts(
        @Param("keyword") String keyword,
        @Param("category") String category,
        @Param("offset") Integer offset,
        @Param("pageSize") Integer pageSize
    );

    int countMarketProducts(
        @Param("keyword") String keyword,
        @Param("category") String category
    );
} 